package com.daxt.controller.filemanager;

import java.io.*;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.common.annotations.OperLog;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.utils.file.DownFileUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.ArchivesTypeMapper;
import com.daxt.model.service.archives.vo.ArchivesType;
import com.daxt.service.MountService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "生成iso")
@RestController
@RequestMapping("/iso")
public class IsoController {

    @Autowired
    private ArchivesTypeMapper archivesTypeMapper;

    //	@Value(value = "${systemConfig.tempPath}")
//	private String basePath1;
    @Autowired
    private MountService mountService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "iso制作结果", notes = "")
    @GetMapping("/createIso/{resultId}")
    public Result<String> mountBatchFile(@PathVariable("resultId") String resultId) throws Exception {
        String result = (String) redisUtil.get(resultId);

        if (!StringUtils.isEmpty(result) && result.contains(":")) {
            String[] split = result.split(":");
            String sysHome = RedisUtil.getSysConfig(SystemConfigType.SYS_HOME);
//			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "1",RedisUtil.getSysConfig(SystemConfigType.OSS_IP).trim()+split[1]);
            return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "1", sysHome + split[1]);
        } else if (!StringUtils.isEmpty(result)) {
            return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), result, null);
        }

        return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "2", null);
    }

    @ApiOperation(value = "创建iso或zip文件", notes = "")
    @PostMapping("/createIso/{typeId}/{model}")
    @OperLog(operModul = "创建iso或zip文件", operType = "ADD", operDesc = "新日志")
    @LoginAuth()
    public Result<String> printTemplet(@ApiIgnore @LoginUser UserInfo loginUser,
            @PathVariable("typeId") String typeId, @ApiParam("0代表件，1代表卷") @PathVariable("model") String model,
            @ApiParam("iso,zip") @RequestParam("fileType") String fileType,
            @ApiParam("分类id") @RequestParam(value = "classId", required = false) String classId,
            @RequestBody List<String> ids,
            HttpServletRequest request, HttpServletResponse response) {
        String path = RedisUtil.getSysConfig(SystemConfigType.TEMP_PATH) + "" + IdWorker.getIdStr().toString();
        String taskId = IdWorker.getIdStr();

        String fondsId=loginUser.getFondsId();

        if (ids != null && ids.size() > 0) {
            ArchivesType selectById = archivesTypeMapper.selectById(typeId);
            if (selectById == null) {
                return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "门类不存在！！！", null);
            }
            File file = new File(path);
            file.mkdirs();

            if ("0".equals(model) || "1".equals(model)) {
                redisUtil.set(taskId, "0", 60 * 60);
                Future future = mountService.createIos(typeId, ids, path, taskId, model, fileType, classId,fondsId);
                try {
                    return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), future.get()+"", taskId);
                }catch (ExecutionException e){
                    e.printStackTrace();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            } else {
                return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "参数异常", null);
            }
        } else {
            return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "请选择要生成镜像的文件", null);
        }

        return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "参数异常", null);
    }


    @ApiOperation(value = "下载iso文件", notes = "")
    @GetMapping("/getIso")
    public void getIso(@RequestParam("path") String path, HttpServletResponse response){

        path=path.replace("\\","/");
        DownFileUtil.download(path,response);

    }

}
